package cn.itcast.nems.base.service.impl;

import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.base.api.SchoolApi;
import cn.itcast.nems.base.dao.entity.ResponsibilityCampusDetail;
import cn.itcast.nems.base.dao.mapper.ResponsibilityCampusDetailMapper;
import cn.itcast.nems.base.dto.SchoolDTO;
import cn.itcast.nems.base.service.ResponsibilityCampusDetailService;
import cn.itcast.nems.common.account.AccountHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 员工归属负责校区明细 服务实现类
 * </p>
 *
 * @author czq
 * @since 2024-02-28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ResponsibilityCampusDetailServiceImpl extends ServiceImpl<ResponsibilityCampusDetailMapper, ResponsibilityCampusDetail> implements ResponsibilityCampusDetailService {
    private final SchoolApi schoolApi;

    @Override
    public Page<ResponsibilityCampusDetail> listPage(Integer pageNumber, Integer pageSize, ResponsibilityCampusDetail params) {

        return Page.of(pageNumber, pageSize, 0, null);
    }

    @Override
    public ResponsibilityCampusDetail getById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    @Transactional
    public String create(String responsibilityCampusId, String campusId) {
        ResponsibilityCampusDetail params = new ResponsibilityCampusDetail();
        params.setResponsibilityCampusId(responsibilityCampusId);
        params.setCampusId(campusId);
        SchoolDTO schoolDTO = schoolApi.findById(campusId);
        params.setCampusName(schoolDTO.getName());
        // 获取用户邮箱
        String email = AccountHolder.getRequiredAccount();
        params.setCreatedBy(email);
        params.setModifiedBy(email);
        params.setId(UUID.randomUUID().toString().replace("-", ""));
        int insert = baseMapper.insert(params);
        if (insert == 1) {
            return params.getId();
        }
        return null;
    }

    @Override
    public Boolean delete(String id) {
        return lambdaUpdate().eq(ResponsibilityCampusDetail::getId, id)
                .set(ResponsibilityCampusDetail::getDeletedBy, AccountHolder.getRequiredAccount())
                .set(ResponsibilityCampusDetail::getDeletedDatetime, LocalDateTime.now())
                .set(ResponsibilityCampusDetail::getDeletedFlag, Boolean.TRUE).update();
    }

    @Override
    public Boolean deleteByResponsibilityCampusId(String responsibilityCampusId) {
        return lambdaUpdate()
                .set(ResponsibilityCampusDetail::getDeletedBy, AccountHolder.getRequiredAccount())
                .set(ResponsibilityCampusDetail::getDeletedDatetime, LocalDateTime.now())
                .eq(ResponsibilityCampusDetail::getResponsibilityCampusId, responsibilityCampusId)
                .set(ResponsibilityCampusDetail::getDeletedFlag, Boolean.TRUE).update();
    }

    @Override
    @Transactional
    public Boolean update(ResponsibilityCampusDetail params) {
        // TODO 代码逻辑....
        int updateById = baseMapper.updateById(params);
        if (updateById == 1) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public List<ResponsibilityCampusDetail> employeeCampusList(String responsibilityCampusId, String defaultCampusId) {
        return baseMapper.employeeCampusList(responsibilityCampusId, defaultCampusId);
    }
}
